<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .customization_toolBar {
            border: none;
        }

        .customization_toolBar .toolBar {
            border: none;
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            height: 20px;
            margin: 15px 0px 0px 0px;
        }

        .customization_toolBar .toolBar .left_tool_div {
            flex-basis: 85%;
            height: 100%;
            display: flex;
            flex-direction: row;
            align-items: center;
        }

        .left_tool_div > * + * {
            margin-left: 10px;
        }

        .customization_toolBar .toolBar .right_tool_div {
            flex-basis: 15%;
            height: 100%;
            display: flex;
            flex-direction: row;
            justify-content: flex-end;
            align-items: center
        }

        .right_tool_div > * + * {
            margin-left: 10px;
        }

        .text-show-btn {
            border: none;
            font-size: 14px;
            color: #666666;
        }

        .text-show-btn:hover {
            color: #666666;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="customization_toolBar">
            <div class="toolBar">
                <div class="left_tool_div">
                    <div>
                        <button class="layui-btn layui-btn-primary layui-btn-sm data-add-btn text-show-btn">
                            已代理商品<label id="agentGoodsQuantity" style="color:#0ab3f1;">(0)</label>
                        </button>
                    </div>
                    <div>
                        <button class="layui-btn layui-btn-primary layui-btn-sm data-add-btn text-show-btn">
                            实际收入<label id="agentSalesRevenue" style="color:#ff0004;">￥0.00</label>
                        </button>
                    </div>
                </div>
                <div class="right_tool_div">

                </div>
            </div>
        </fieldset>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/common/commonApi.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script>
    let userId = '';
    window.onload = function () {
        userId = GetQueryString("userId");
        if (isNotNull(userId)) {
            init();
        } else {
            layer.msg('数据异常，请重试');
            setTimeout(function () {
                let iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);
            }, 1500);
        }
    }

    function init() {
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;

            table.render({
                elem: '#currentTableId',
                url: '/buying/publish/group/purchase/list',
                where: {
                    initiatorId: userId
                },
                method: 'GET',
                dataType: 'json',
                contentType: 'application/x-www-form-urlencoded',
                toolbar: false,
                cols: [[
                    {
                        field: 'numbering', title: '序号', width: 60, align: 'center', templet: function (d) {
                            return d.LAY_INDEX; //返回序号
                        }
                    },
                    {
                        field: 'goodsName', title: '代理销售商品名称', sort: true, align: 'center', templet: function (d) {
                            return d.goodsName + '(' + d.goodsNo + ')'
                        }
                    },
                    {
                        field: 'distributionReward',
                        title: '代理销售佣金(元)',
                        sort: true,
                        align: 'center',
                        templet: function (d) {
                            let num = parseFloat(d.distributionReward)
                            return num.toFixed(2);
                        }
                    },
                    {field: 'usedQuantity', title: '累计销量', sort: true, align: 'center'},
                    {
                        field: 'actualIncome',
                        title: '实际收入(元)',
                        sort: true,
                        align: 'center',
                        templet: function (d) {
                            let usedQuantity = isNotNull(d.usedQuantity) ? d.usedQuantity : 0.00;
                            usedQuantity = parseInt(usedQuantity)
                            let distributionReward = isNotNull(d.distributionReward) ? d.distributionReward : 0.00;
                            distributionReward = parseFloat(distributionReward).toFixed(2);
                            let num = usedQuantity * distributionReward
                            return num.toFixed(2);
                        }
                    },
                ]],
                limits: [20, 30, 40, 50, 100],
                limit: 20,
                page: true,
                request: {
                    pageName: 'pageNum',
                    limitName: 'pageSize'
                },
                response: { //定义后端 json 格式，详细参见官方文档
                    statusName: 'code', //状态字段名称
                    statusCode: '200', //状态字段成功值
                    msgName: 'msg', //消息字段
                    countName: 'total', //总数字段
                    dataName: 'rows' //数据字段
                },
            });
            getPublishGroupPurchaseData();

        });
    }

    function getPublishGroupPurchaseData() {
        layui.use(['form'], function () {
            let form = layui.form,
                $ = layui.$;
            $.ajax({
                type: "GET",
                url: `/buying/publish/group/purchase/data/${userId}`,
                contentType: 'application/x-www-form-urlencoded',
                dataType: 'json',
                success: function (req) {
                    if (req.code === '200') {
                        let data = req.data;
                        if (data) {
                            const myMap = new Map(Object.entries(data));
                            // 更新统计数据
                            myMap.forEach((value, key) => {
                                let id = '#' + key
                                if (key === 'agentGoodsQuantity') {
                                    $(id).text('(' + value + ')');
                                } else {
                                    $(id).text('￥' + value);
                                }

                            });
                        }
                    } else {
                        layer.msg(req.msg);
                    }
                },
                error: function (e) {
                    layer.msg(e.msg);
                }
            })
        });
    }

    /**
     * 重载表格数据
     */
    function reloadTable() {
        layui.use(['table'], function () {
            let table = layui.table;
            table.reload('currentTableId', {
                url: '/buying/publish/group/purchase/list',
            });
        })
    }

</script>

</body>
</html>